package cn.edu.pku.sei.ra.arch.C2.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;

import cn.edu.pku.sei.ra.arch.C2.Architecture;
import cn.edu.pku.sei.ra.arch.C2.C2Package;
import cn.edu.pku.sei.ra.arch.C2.Component;
import cn.edu.pku.sei.ra.arch.C2.Connector;
import cn.edu.pku.sei.ra.arch.C2.diagram.edit.parts.ArchitectureEditPart;
import cn.edu.pku.sei.ra.arch.C2.diagram.edit.parts.ComponentAboveEditPart;
import cn.edu.pku.sei.ra.arch.C2.diagram.edit.parts.ComponentBelowEditPart;
import cn.edu.pku.sei.ra.arch.C2.diagram.edit.parts.ComponentEditPart;
import cn.edu.pku.sei.ra.arch.C2.diagram.edit.parts.ConnectorEditPart;
import cn.edu.pku.sei.ra.arch.C2.diagram.providers.C2ElementTypes;

/**
 * @generated
 */
public class C2DiagramUpdater {

	/**
	 * @generated
	 */
	public static List getSemanticChildren(View view) {
		switch (C2VisualIDRegistry.getVisualID(view)) {
		case ArchitectureEditPart.VISUAL_ID:
			return getArchitecture_79SemanticChildren(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getArchitecture_79SemanticChildren(View view) {
		if (!view.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		Architecture modelElement = (Architecture) view.getElement();
		List result = new LinkedList();
		for (Iterator it = modelElement.getComponent().iterator(); it.hasNext();) {
			Component childElement = (Component) it.next();
			int visualID = C2VisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == ComponentEditPart.VISUAL_ID) {
				result.add(new C2NodeDescriptor(childElement, visualID));
				continue;
			}
		}
		for (Iterator it = modelElement.getConnector().iterator(); it.hasNext();) {
			Connector childElement = (Connector) it.next();
			int visualID = C2VisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == ConnectorEditPart.VISUAL_ID) {
				result.add(new C2NodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getContainedLinks(View view) {
		switch (C2VisualIDRegistry.getVisualID(view)) {
		case ArchitectureEditPart.VISUAL_ID:
			return getArchitecture_79ContainedLinks(view);
		case ComponentEditPart.VISUAL_ID:
			return getComponent_1001ContainedLinks(view);
		case ConnectorEditPart.VISUAL_ID:
			return getConnector_1002ContainedLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getIncomingLinks(View view) {
		switch (C2VisualIDRegistry.getVisualID(view)) {
		case ComponentEditPart.VISUAL_ID:
			return getComponent_1001IncomingLinks(view);
		case ConnectorEditPart.VISUAL_ID:
			return getConnector_1002IncomingLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getOutgoingLinks(View view) {
		switch (C2VisualIDRegistry.getVisualID(view)) {
		case ComponentEditPart.VISUAL_ID:
			return getComponent_1001OutgoingLinks(view);
		case ConnectorEditPart.VISUAL_ID:
			return getConnector_1002OutgoingLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getArchitecture_79ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getComponent_1001ContainedLinks(View view) {
		Component modelElement = (Component) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getOutgoingFeatureModelFacetLinks_Component_Below_3001(modelElement));
		result
				.addAll(getOutgoingFeatureModelFacetLinks_Component_Above_3002(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getConnector_1002ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getComponent_1001IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getConnector_1002IncomingLinks(View view) {
		Connector modelElement = (Connector) view.getElement();
		Map crossReferences = EcoreUtil.CrossReferencer.find(view.eResource()
				.getResourceSet().getResources());
		List result = new LinkedList();
		result.addAll(getIncomingFeatureModelFacetLinks_Component_Below_3001(
				modelElement, crossReferences));
		result.addAll(getIncomingFeatureModelFacetLinks_Component_Above_3002(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getComponent_1001OutgoingLinks(View view) {
		Component modelElement = (Component) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getOutgoingFeatureModelFacetLinks_Component_Below_3001(modelElement));
		result
				.addAll(getOutgoingFeatureModelFacetLinks_Component_Above_3002(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getConnector_1002OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	private static Collection getIncomingFeatureModelFacetLinks_Component_Below_3001(
			Connector target, Map crossReferences) {
		Collection result = new LinkedList();
		Collection settings = (Collection) crossReferences.get(target);
		for (Iterator it = settings.iterator(); it.hasNext();) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) it
					.next();
			if (setting.getEStructuralFeature() == C2Package.eINSTANCE
					.getComponent_Below()) {
				result.add(new C2LinkDescriptor(setting.getEObject(), target,
						C2ElementTypes.ComponentBelow_3001,
						ComponentBelowEditPart.VISUAL_ID));
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getIncomingFeatureModelFacetLinks_Component_Above_3002(
			Connector target, Map crossReferences) {
		Collection result = new LinkedList();
		Collection settings = (Collection) crossReferences.get(target);
		for (Iterator it = settings.iterator(); it.hasNext();) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) it
					.next();
			if (setting.getEStructuralFeature() == C2Package.eINSTANCE
					.getComponent_Above()) {
				result.add(new C2LinkDescriptor(setting.getEObject(), target,
						C2ElementTypes.ComponentAbove_3002,
						ComponentAboveEditPart.VISUAL_ID));
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getOutgoingFeatureModelFacetLinks_Component_Below_3001(
			Component source) {
		Collection result = new LinkedList();
		Connector destination = source.getBelow();
		if (destination == null) {
			return result;
		}
		result.add(new C2LinkDescriptor(source, destination,
				C2ElementTypes.ComponentBelow_3001,
				ComponentBelowEditPart.VISUAL_ID));
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getOutgoingFeatureModelFacetLinks_Component_Above_3002(
			Component source) {
		Collection result = new LinkedList();
		for (Iterator destinations = source.getAbove().iterator(); destinations
				.hasNext();) {
			Connector destination = (Connector) destinations.next();
			result.add(new C2LinkDescriptor(source, destination,
					C2ElementTypes.ComponentAbove_3002,
					ComponentAboveEditPart.VISUAL_ID));
		}
		return result;
	}

}
